package demo.android.ipc;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.method.DigitsKeyListener;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;

import com.google.gson.Gson;
import com.hss01248.facedetect.BaseURL;
import com.hss01248.facedetect.MyApplication;
import com.hss01248.facedetect.response.LoginResponse;
import com.hss01248.facedetect.response.UserloginResponse;
import com.hss01248.facedetect.utils.MyProgressUtils;
import com.hss01248.facedetect.utils.SPUtils;
import com.hss01248.facedetect.utils.ToastUtil;
import com.hss01248.facedetect.utils.httpUtils;

import org.json.JSONObject;

import java.lang.reflect.Method;

import demo.android.ipc.my_view.RxDialogEditSureCancel;

public class LoginActivity extends AppCompatActivity implements View.OnClickListener {
    private EditText et_userPwd;
    private EditText et_userName;
    private Button btn_login;
    SharedPreferences sp;
    SharedPreferences.Editor edit;
    private ImageView img_ip;
    String url;
    String http="http://";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        supportRequestWindowFeature(Window.FEATURE_NO_TITLE); // 去掉标题栏 有效
        setContentView(R.layout.activity_login);

        sp = getSharedPreferences("position", Context.MODE_PRIVATE);
        edit = sp.edit();

        et_userName = (EditText) findViewById(R.id.et_lg_user);
        et_userPwd = (EditText) findViewById(R.id.et_lg_pwd);
        btn_login = (Button) findViewById(R.id.btn_lg_login);
        img_ip = (ImageView) findViewById(R.id.img_ip);


//        if (sp.getString("userName", "") != null) {
////            获取到存储的值
//            et_userName.setText(sp.getString("userName", ""));
////            et_userPwd.setText(sp.getString("password", ""));
//        }


        if (sp.getString("ip", "").equals("")) {
            ToastUtil.showToast("请先设置ip地址");
        } else {
            //获取到存储的值
//            et_userName.setText(sp.getString("userName", ""));
//            et_userPwd.setText(sp.getString("password", ""));
        }

        img_ip.setOnClickListener(this);
        btn_login.setOnClickListener(this);
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_lg_login:
                if (getSharedPreferences("position", Context.MODE_PRIVATE).getString("ip", "").equals("")) {
                    ToastUtil.showToast("请先设置IP地址");
                } else {
                    login();
                }
                break;
            case R.id.img_ip:

                setIp();
                break;
        }
    }

    private void setIp() {
        //提示弹窗
        final RxDialogEditSureCancel rxDialogEditSureCancel = new RxDialogEditSureCancel(LoginActivity.this);
        rxDialogEditSureCancel.getTitleView().setText("请输入IP地址");
//                若已经保存ip，则显示
        if (!sp.getString("ip", "").equals("")) {
            rxDialogEditSureCancel.getEditText().setText(sp.getString("ip", ""));
        }
        rxDialogEditSureCancel.getEditText().setHint("192.168.8.8:8888/Base");
        String digital = "0123456789.:/qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM";
        rxDialogEditSureCancel.getEditText().setKeyListener(DigitsKeyListener.getInstance(digital));


        rxDialogEditSureCancel.getSureView().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                edit.putString("ip", rxDialogEditSureCancel.getEditText().getText().toString().trim());
                edit.commit();

                ToastUtil.showToast("设置IP: " + rxDialogEditSureCancel.getEditText().getText().toString() + " 成功");
                rxDialogEditSureCancel.cancel();
            }
        });
        rxDialogEditSureCancel.getCancelView().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                rxDialogEditSureCancel.cancel();
            }
        });

        rxDialogEditSureCancel.show();
    }

    public void login() {
        final String userName = et_userName.getText().toString().trim();
        final String pwd = et_userPwd.getText().toString().trim();
        if (userName.equals("")) {
            ToastUtil.showToast("请输入账号");
            return;
        }
        if (pwd.equals("")) {
            ToastUtil.showToast("请输入密码");
            return;
        }
        MyProgressUtils.showProgressDialog(LoginActivity.this);
        new Thread(new Runnable() {
            @Override
            public void run() {


                // 先封装一个 JSON 对象
                JSONObject param = new JSONObject();

                try {
                    param.put("userName", userName);
                    param.put("password", pwd);
                    param.put("deskTerminalNo", getSerialNumber());

                    param.put("sign", "A96D48AE69F46BD0E1B17F10316086F3");
                    param.put("timestamp", "20180611165000");


                    String ss = httpUtils.postJson(http+sp.getString("ip","")+BaseURL.url_userLogin, param);

                    UserloginResponse userLoginResponse = new Gson().fromJson(ss, UserloginResponse.class);
                    MyProgressUtils.dismissProgress();
                    if (userLoginResponse.getCode().equals("0")) {
//                        edit.putString("userName", userName);
//                        edit.putString("password", pwd);
//                        edit.apply();

//                        MyApplication.USER_ID =String.valueOf(userLoginResponse.getData().getId()) ;
//                        MyApplication.CHECK_TICKET_TERMINAL_N0 = userLoginResponse.getData().getGateNo();
//                        MyApplication.SALE_TICKET_TERMINAL_ID = String.valueOf(userLoginResponse.getData().getTerminalId());

                        SPUtils.put(LoginActivity.this,"url",String.valueOf(http+sp.getString("ip","")));
                        SPUtils.put(LoginActivity.this,"USER_ID",String.valueOf(userLoginResponse.getData().getId()));
                        SPUtils.put(LoginActivity.this,"CHECK_TICKET_TERMINAL_N0",String.valueOf(userLoginResponse.getData().getGateNo()));
                        SPUtils.put(LoginActivity.this,"SALE_TICKET_TERMINAL_ID",String.valueOf(userLoginResponse.getData().getTerminalId()));


                        startActivity(new Intent(LoginActivity.this, ChoiceActivity.class));
                        finish();
                    } else {
                        ToastUtil.showToast("登录失败:"+userLoginResponse.getMessage());
                    }
                } catch (Exception e) {
                    MyProgressUtils.dismissProgress();
                    ToastUtil.showToast("登录异常：" + e.getMessage());
                }
            }
        }).start();

    }

    private String getSerialNumber() {
        String serial = null;
        try {
            Class<?> c = Class.forName("android.os.SystemProperties");
            Method get = c.getMethod("get", String.class);
            serial = (String) get.invoke(c, "ro.serialno");
        } catch (Exception e) {
            e.printStackTrace();
        }

        return serial;

    }

}
